<template>
	<view class="downTime">
		<text class="" @click.stop="getVerify">{{verifyText}}</text>
	</view>
</template>

<script>
	import { mobile } from "@/utils/verification.js"
	import { getSmsCode } from "@/api/index.js"
	import { setInterval } from "timers";
	export default {
		name:"uni-downtime",
		props:{
			tel:{
				type:String,
				default:''
			},
			time:{
				type:[String,Number],
				default:60
			},
			msgType:{
				type:String,
				default:'LOGIN'
			}
		},
		data() {
			return {
				verifyText:'获取验证码',
				canClick:true
			};
		},
		methods:{
			// 获取验证码
			getVerify(){
				if(!(mobile.test(this.tel))){
					uni.showToast({
						icon:'none',
						title:'请填写标准手机号'
					})
					return
				}else{
					if(this.verifyText.indexOf('s')!=-1){
						uni.showToast({
							icon:'none',
							title:'请勿短时间内重复获取验证码'
						})
						return
					}
					if(this.canClick){
						this.canClick=false
						let param={
							msgType: this.msgType,
							phoneNumber:this.tel,
							project:"KWKD"
						}
						getSmsCode(param).then((res)=>{
							this.canClick=true
							if(res.code==100000){
								uni.showToast({
									icon:'none',
									title:'验证码发送成功,请注意查收'
								})
								this.downTime(this.time)
							}
						}).catch((err)=>{this.canClick=true})
					}
				}
			},
			downTime(num){
				let downNum=num
				downNum--
				this.canClick=true
				setTimeout(()=>{
					if(downNum>0){
						this.verifyText=downNum+'s'
						this.downTime(downNum)
					}else{
						this.verifyText='重新获取验证码';
					}
				},1000)
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>